package com.heatup.apt.management.service.impl;

import com.baomidou.mybatisplus.mapper.*;
import com.heatup.apt.management.service.*;
import com.heatup.apt.common.constants.OrderContants;
import com.heatup.apt.common.constants.TemplateConstants;
import com.heatup.apt.common.dao.CourseScheduleMapper;
import com.heatup.apt.common.dao.OrderInfoResultMapper;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.common.dao.redis.TokenRedisDao;
import com.heatup.apt.common.dao.store.StoreInfoMapper;
import com.heatup.apt.common.dao.user.UserDiscountMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.common.util.SendTemplateThread;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.model.api.OrderInfoResultModel;
import com.heatup.apt.model.course.CourseSchedule;
import com.heatup.apt.model.order.OrderDetail;
import com.heatup.apt.model.order.OrderInfo;
import com.heatup.apt.model.store.StoreInfo;
import com.heatup.apt.model.user.UserDiscount;
import com.heatup.apt.model.user.UserInfo;
import com.heatup.apt.model.user.UserProductService;
import com.heatup.apt.model.weixin.Template;
import com.heatup.apt.model.weixin.TemplateParam;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.text.*;
import java.util.*;


@Service
public class TemplateSendServiceImpl implements TemplateSendService {
	private static final Logger logger = LoggerFactory.getLogger(TemplateSendServiceImpl.class);
	@Autowired
	private TokenMapper tokenMapper;
	@Autowired
	private TokenRedisDao tokenRedisDao;
	@Autowired
	private OrderInfoResultMapper orderInfoResultMapper;
	@Autowired
	private CourseScheduleMapper courseScheduleMapper;
	@Autowired
	private UserDiscountMapper userDiscountMapper;
	@Autowired
	private StoreInfoMapper storeInfoMapper;

	/**
	 * 预约排队通知
	 */
	@Override
	public void sendOrderWaitTemplateMsg(OrderInfo orderInfo) {
		logger.info("==================== sendWaitSuccTemplateMsg start==========================");
		OrderInfoResultModel orderInfoResult = (OrderInfoResultModel) orderInfo;
		Map<Object, Object> beforeParams = new HashMap<Object, Object>();
		beforeParams.put("orderId", orderInfoResult.getId());
		beforeParams.put("serviceId", orderInfoResult.getServiceId());
		int beforeWaitNum = orderInfoResultMapper.queryBeforeWaitNum(beforeParams);
		String tourl = MessageFormat.format(TemplateConstants.TEMPLATE_TO_URL_ORDER, orderInfo.getOrderType(),
				String.valueOf(orderInfo.getId()));
		try {
			Template template = new Template();
			template.setToUser(orderInfoResult.getUserInfo().getOpenid());
			template.setTemplateId(TemplateConstants.USER_WAIT_SUCCESS_TIPS);
			template.setTopColor("#333");
			template.setUrl(tourl);
			List<TemplateParam> paras = new ArrayList<TemplateParam>();
			paras.add(new TemplateParam("first", "您好，您已支付成功，前面还有" + beforeWaitNum + "人排队，如有人取消您将排队成功。", "#333"));
			paras.add(new TemplateParam("keyword1", orderInfoResult.getServicePrice().getServiceName(), "#333"));
			paras.add(new TemplateParam("keyword2", getOrderStartTimeByOrderType(orderInfoResult), "#333"));
			paras.add(new TemplateParam("remark", "如到达开课时间仍未排队成功，系统将自动取消订单并给您退款。如有任何疑问，可联系客服：0755-86629280。", "#333"));
			template.setTemplateParamList(paras);
			new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
		logger.info("==================== sendWaitSuccTemplateMsg end==========================");
	}

	/**
	 * @author EarvinWoo 取消订单模板消息接口
	 * @param backMoneyFlag
	 *            1:需要退款
	 * @param orderId
	 *            ：订单ID
	 */
	@Override
	public void sendCancelOrderTemplateMsg(String backMoneyFlag, Integer orderId) {

		logger.info("==================== sendCancelOrderTemplateMsg start==========================");
		logger.info("SendCancelOrderTemplateMsg Method Param is " + " backMoneyFlag:" + backMoneyFlag + " orderId:"
				+ orderId);
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			OrderInfoResultModel orderInfo = orderInfoResultMapper.selectByOrderId(orderId);
			String tourl = MessageFormat.format(TemplateConstants.TEMPLATE_TO_URL_ORDER, orderInfo.getOrderType(),
					String.valueOf(orderInfo.getId()));
			int orderType = orderInfo.getOrderType();
			// 订单类型(0普通订单 1团课订单 2私教订单 3趣营订单 4自助健身 5会员订单)
			String orderTypeName = conveterOrderTypeName(orderType);
			Template template = new Template();
			template.setToUser(orderInfo.getUserInfo().getOpenid());
			template.setTemplateId(TemplateConstants.USER_ORDER_CANCEL_TIPS);
			template.setTopColor("#333");
			template.setUrl(tourl);
			List<TemplateParam> paras = new ArrayList<TemplateParam>();
			// 根据是否退款来显示不同信息，1表示可退
			if ("1".equals(backMoneyFlag)) {
				paras.add(new TemplateParam("first", "您好，您的订单取消成功！符合退款条件的订单金额将即时退回您的代金卡账户，请查看。", "#333"));
				paras.add(new TemplateParam("keyword5", String.valueOf(orderInfo.getAmount()), "#333"));
				paras.add(new TemplateParam("remark",
						"自助健身开始前6小时内取消预订将不予退款，团课在开始前24小时内取消预订将不予退款；训练营开始前48小时内取消预订将不予退款。如有任何疑问，可联系客服：0755-86629280。", "#333"));
			} else {
				paras.add(
						new TemplateParam("first", "您好，您的订单取消成功！自助健身开始前6小时内取消预订将不予退款，团课在开始前24小时内取消预订将不予退款；训练营开始前48小时内取消预订将不予退款。", "#333"));
				paras.add(new TemplateParam("keyword5", "0", "#333"));
				paras.add(new TemplateParam("remark", "如有任何疑问，可联系客服：0755-86629280。", "#333"));
			}
			paras.add(new TemplateParam("keyword1", orderInfo.getOrderNumber(), "#333"));
			paras.add(new TemplateParam("keyword2", orderTypeName, "#333"));
			paras.add(new TemplateParam("keyword3", sdf.format(orderInfo.getCreatedTime()), "#333"));// 下单时间
			paras.add(new TemplateParam("keyword4", sdf.format(new Date()), "#333"));// 取消时间
			template.setTemplateParamList(paras);
			new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info("==================== sendCancelOrderTemplateMsg end==========================");

	}

	/**
	 * @author EarvinWoo
	 */
	@Override
	public void sendBookingSuccTemplateMsg(Integer orderId, String orderStatus) {
		// TODO Auto-generated method stub

		logger.info("==================== sendBookingSuccTemplateMsg start==========================");
		logger.info("sendBookingSuccTemplateMsg Method Param is " + " orderId:" + orderId);
		try {
			OrderInfoResultModel orderInfo = orderInfoResultMapper.selectByOrderId(orderId);
			String tourl = MessageFormat.format(TemplateConstants.TEMPLATE_TO_URL_ORDER, orderInfo.getOrderType(),
					String.valueOf(orderInfo.getId()));
			if (orderInfo != null) {
				Template template = new Template();
				template.setToUser(orderInfo.getUserInfo().getOpenid());
				template.setTemplateId(TemplateConstants.ORDER_BOOK_SUCCESS_TIPS);
				template.setTopColor("#333");
				template.setUrl(tourl);
				List<TemplateParam> paras = new ArrayList<TemplateParam>();
				if (OrderContants.OrderStatus.WAIT.getStatus().equals(orderStatus)) {
					paras.add(new TemplateParam("first", "亲，您的排队订单已排队成功！课程开始前我们将及时通知您，请保持手机畅通！！", "#333"));
				} else {
					paras.add(new TemplateParam("first", "亲，您的订单已支付成功！课程开始前我们将及时通知您，请保持手机畅通！", "#333"));
				}
				paras.add(new TemplateParam("keyword1", orderInfo.getOrderNumber(), "#333"));
				paras.add(new TemplateParam("keyword2", conveterOrderTypeName(orderInfo.getOrderType()), "#333"));
				paras.add(new TemplateParam("keyword3", orderInfo.getServicePrice().getServiceName(), "#333"));
				paras.add(new TemplateParam("keyword4", orderInfo.getAmount().toString(), "#333"));
				paras.add(new TemplateParam("keyword5", getOrderStartTimeByOrderType(orderInfo), "#333"));
				paras.add(new TemplateParam("remark",
						"自助健身开始前6小时内取消预订将不予退款，团课在开始前24小时内取消预订将不予退款；训练营开始前48小时内取消预订将不予退款。如有任何疑问，可联系客服：0755-86629280。", "#333"));
				template.setTemplateParamList(paras);
				new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();

			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
		logger.info("==================== sendBookingSuccTemplateMsg end==========================");
	}

	@Override
	public void sendOpenVIPSuccTemplateMsg(Integer orderId) {
		logger.info("==================== sendOpenVIPSuccTemplateMsg start==========================");
		logger.info("sendBookingSuccTemplateMsg Method Param is orderId:" + orderId);
		try {

			// String tourl
			// ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx65db45e85c1be9c4&redirect_uri=http%3A%2F%2Fh5.sincebest.com%2Fapi%2Fredirect%2FweiXinJump%3Fpath%3D%2Fopen_vip%26scopetepm%3Dsnsapi_userinfo&response_type=code&scope=snsapi_userinfo&state=321#wechat_redirect";
			String tourl = TemplateConstants.TEMPLATE_TO_URL_VIP_ORDER;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			OrderInfoResultModel orderInfo = orderInfoResultMapper.selectByOrderId(orderId);
			UserInfo userInfo = orderInfo.getUserInfo();
			UserDiscount userDiscount = userDiscountMapper.selectById(orderInfo.getServiceId());
			String orderTypeName = "";
			String vaildTimePeriod = "";
			if (userDiscount != null) {
				orderTypeName = userDiscount.getRemarks();
			}
			try {
				vaildTimePeriod = sdf.format(userInfo.getVipStartTime()) + "至" + sdf.format(userInfo.getVipEndTime());
			} catch (Exception e) {
				logger.info(e.getMessage());
			}

			Template template = new Template();
			template.setToUser(orderInfo.getUserInfo().getOpenid());
			template.setTemplateId(TemplateConstants.OPEN_VIP_USER_TIPS);
			template.setTopColor("#333");
			template.setUrl(tourl);
			List<TemplateParam> paras = new ArrayList<TemplateParam>();
			paras.add(new TemplateParam("first", "您好，您已成功开通围裙健身会员！", "#333"));
			paras.add(new TemplateParam("keyword1", orderInfo.getOrderNumber(), "#333"));
			paras.add(new TemplateParam("keyword2", "围裙健身会员", "#333"));
			paras.add(new TemplateParam("keyword3", orderInfo.getAmount().toString(), "#333"));
			paras.add(new TemplateParam("keyword4", orderTypeName, "#333"));
			paras.add(new TemplateParam("keyword5", vaildTimePeriod, "#333"));
			paras.add(new TemplateParam("remark", "如有任何疑问，可联系客服：0755-86629280。", "#333"));
			template.setTemplateParamList(paras);

			new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info("==================== sendOpenVIPSuccTemplateMsg end==========================");

	}

	/**
	 * @author EarvinWoo
	 * @param orderInfo
	 * @see
	 */
	@Override
	public void sendUpCommingTemplateMsg(OrderInfo orderInfo) {
		logger.info("==========sendUpCommingTemplateMsg start==========");
		try {
			OrderInfoResultModel orderInfoResult = (OrderInfoResultModel) orderInfo;
			StoreInfo storeInfo = getStoreInfo(orderInfoResult);
			if (orderInfoResult != null) {
				// String
				// tourl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx65db45e85c1be9c4&redirect_uri=http%3A%2F%2Fh5.sincebest.com%2Fapi%2Fredirect%2FweiXinJump%3Fpath%3D%2Forder_details%3ForderType%3D
				// " +orderInfo.getOrderType()+
				// "%26orderId%3D"+orderInfo.getId()+"%26scopetepm%3Dsnsapi_userinfo&response_type=code&scope=snsapi_userinfo&state=321#wechat_redirect";
				String tourl = MessageFormat.format(TemplateConstants.TEMPLATE_TO_URL_ORDER, orderInfo.getOrderType(),
						String.valueOf(orderInfo.getId()));
				Template template = new Template();
				template.setToUser(orderInfoResult.getUserInfo().getOpenid());
				template.setTemplateId(TemplateConstants.COURSE_START_TIPS);
				template.setTopColor("#333");
				template.setUrl(tourl);
				List<TemplateParam> paras = new ArrayList<TemplateParam>();
				paras.add(new TemplateParam("first", "您好，您报名的课程即将开始，进店密码将在开始前20分钟显示。", "#333"));
				paras.add(new TemplateParam("keyword1", conveterCourseName(orderInfoResult), "#333"));// 课程名称
				paras.add(new TemplateParam("keyword2",
						storeInfo.getStoreName() == null ? "" : storeInfo.getStoreName(), "#333"));// 门店名称
				paras.add(new TemplateParam("keyword3", storeInfo.getAddress() == null ? "" : storeInfo.getAddress(),
						"#333"));// 门店地址
				paras.add(new TemplateParam("keyword4", getOrderStartTimeByOrderType(orderInfoResult), "#333"));// 课程时间
				paras.add(new TemplateParam("remark", "请准备好装备，一起燃烧卡路里吧！如有任何疑问，可联系客服：0755-86629280。", "#333"));
				template.setTemplateParamList(paras);
				new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
			}

		} catch (Exception ex) {
			logger.error(ex.getMessage());
		}
		logger.info("==========sendUpCommingTemplateMsg end==========");
	}

	private String conveterOrderTypeName(int orderType) {
		String orderTypeName = "";
		if (orderType == OrderContants.OrderType.team_course.getIndex()) {
			orderTypeName = OrderContants.OrderType.team_course.getTitle();
		} else if (orderType == OrderContants.OrderType.fun_camp.getIndex()) {
			orderTypeName = OrderContants.OrderType.fun_camp.getTitle();
		} else if (orderType == OrderContants.OrderType.selffit.getIndex()) {
			orderTypeName = OrderContants.OrderType.selffit.getTitle();
		} else if (orderType == OrderContants.OrderType.pt.getIndex()) {
			orderTypeName = OrderContants.OrderType.pt.getTitle();
		} else if (orderType == OrderContants.OrderType.vip_order.getIndex()) {
			orderTypeName = OrderContants.OrderType.vip_order.getTitle();
		}
		return orderTypeName;
	}

	/**
	 * 转换课程名称信息
	 * 
	 * @param orderInfo
	 * @return
	 */
	private String conveterCourseName(OrderInfoResultModel orderInfo) {
		int orderType = orderInfo.getOrderType();
		String courseName = "";
		if (orderType == OrderContants.OrderType.team_course.getIndex()) {
			courseName = orderInfo.getServicePrice().getServiceName();
		} else if (orderType == OrderContants.OrderType.fun_camp.getIndex()) {
			courseName = orderInfo.getServicePrice().getServiceName();
		} else if (orderType == OrderContants.OrderType.selffit.getIndex()) {
			courseName = orderInfo.getServicePrice().getServiceName();
		} else if (orderType == OrderContants.OrderType.pt.getIndex()) {
			courseName = orderInfo.getServicePrice().getServiceName();
		} else if (orderType == OrderContants.OrderType.vip_order.getIndex()) {
			courseName = "围裙健身会员";
		}
		return courseName;
	}

	/**
	 * 根据订单类型不同，取不同的订单开始时间的模板消息显示信息
	 * 
	 * @param orderInfo
	 * @return 订单开始时间
	 */
	private String getOrderStartTimeByOrderType(OrderInfoResultModel orderInfo) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String courseStartTime = "课程开始时间请查看订单详情信息";
		try {
			// 根据课程类型不同，去不同的课程开始时间
			if (StringUtil.isEquals(OrderContants.OrderType.fun_camp.getType(), orderInfo.getOrderType().toString())
					|| StringUtil.isEquals(OrderContants.OrderType.team_course.getType(),
							orderInfo.getOrderType().toString())) {
//				CourseScheduleExample courseScheduleExample = new CourseScheduleExample();
//				courseScheduleExample.or().andCourseIdEqualTo(orderInfo.getServicePrice().getCourseId());
//				List<CourseSchedule> courseSchedules = courseScheduleMapper.selectByExample(courseScheduleExample);

                CourseSchedule courseScheduleDB = new CourseSchedule();
                courseScheduleDB.setCourseId(orderInfo.getServicePrice().getCourseId());
                EntityWrapper<CourseSchedule> entityWrapper = new EntityWrapper<>(courseScheduleDB);
                List<CourseSchedule> courseSchedules = courseScheduleMapper.selectList(entityWrapper);

				CourseSchedule courseSchedule = null;
				if (courseSchedules != null && courseSchedules.size() > 0) {
					courseSchedule = courseSchedules.get(0);
				}
				Date startTime = courseSchedule.getStartTime();
				if (startTime != null) {
					courseStartTime = sdf.format(startTime);
				}
			} else if (StringUtil.isEquals(OrderContants.OrderType.pt.getType(), orderInfo.getOrderType().toString())) {
				courseStartTime = "请与教练确认具体上课时间";
			} else if (StringUtil.isEquals(OrderContants.OrderType.selffit.getType(),
					orderInfo.getOrderType().toString())) {
				OrderDetail orderDetail = orderInfo.getOrderDetail();
				if (orderDetail != null) {
					Date startTime = orderDetail.getStartTime();
					if (startTime != null)
						courseStartTime = sdf.format(orderDetail.getStartTime());
				}
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
		return courseStartTime;
	}

	private StoreInfo getStoreInfo(OrderInfoResultModel orderInfo) {
		if (orderInfo.getOrderType() == OrderContants.OrderType.vip_order.getIndex()) {
			return null;
		} else {
			return storeInfoMapper.selectById(orderInfo.getOrderDetail().getStoreId());
		}
	}

	/**
	 * 自动取消已排队订单的任务
	 */
	@Override
	public void sendAutoCancelWaitOrderTemplate(String backMoneyFlag, OrderInfo orderInfo) {
		logger.info("==================== sendAutoCancelWaitOrderTemplate start==========================");
		logger.info("sendAutoCancelWaitOrderTemplate Method Param is " + " backMoneyFlag:" + backMoneyFlag + " orderId:"
				+ orderInfo.getId());
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			OrderInfoResultModel orderInfoResult = (OrderInfoResultModel) orderInfo;
			String tourl = MessageFormat.format(TemplateConstants.TEMPLATE_TO_URL_ORDER, orderInfo.getOrderType(),
					String.valueOf(orderInfo.getId()));
			int orderType = orderInfoResult.getOrderType();
			// 订单类型(0普通订单 1团课订单 2私教订单 3趣营订单 4自助健身 5会员订单)
			String orderTypeName = conveterOrderTypeName(orderType);
			Template template = new Template();
			template.setToUser(orderInfoResult.getUserInfo().getOpenid());
			template.setTemplateId(TemplateConstants.USER_ORDER_CANCEL_TIPS);
			template.setTopColor("#333");
			template.setUrl(tourl);
			List<TemplateParam> paras = new ArrayList<TemplateParam>();
			String first ="您好，您的订单未排队成功，我们只能忍痛为您取消订单。";
			// 根据是否退款来显示不同信息，1表示可退
			if ("1".equals(backMoneyFlag)) {
				if(orderInfo.getOrderType().intValue() == OrderContants.OrderType.team_course.getIndex()){
					first="您好，您预约的课程在开课后仍未排队成功，我们只能忍痛为您取消订单。喜欢的课程，下次记得要赶早预约哦!";
				}else if(orderInfo.getOrderType().intValue()==OrderContants.OrderType.fun_camp.getIndex()){
					first="您好，您预约的课程在开课后仍未排队成功，我们只能忍痛为您取消订单，订单金额将在5个工作日内退回您的代金卡账户。喜欢的课程，下次记得要赶早预约哦!";
				}
				paras.add(new TemplateParam("keyword5", String.valueOf(orderInfoResult.getAmount()), "#333"));

			} else {
				if(orderInfo.getOrderType().intValue()==OrderContants.OrderType.team_course.getIndex()){
					first="您好，您预约的课程在开课后仍未排队成功，我们只能忍痛为您取消订单。喜欢的课程，下次记得要赶早预约哦!";
				}else if(orderInfo.getOrderType().intValue()==OrderContants.OrderType.fun_camp.getIndex()){
					first="您好，您预约的课程在开课后仍未排队成功，我们只能忍痛为您取消订单，训练营成营后，开课前48小时内取消预订将不予退款。";
				}
				paras.add(new TemplateParam("keyword5", "0", "#333"));
			}
			paras.add(new TemplateParam("remark",
					"如有任何疑问，可联系客服：0755-86629280。", "#333"));
			paras.add(new TemplateParam("first", first, "#333"));
			paras.add(new TemplateParam("keyword1", orderInfoResult.getOrderNumber(), "#333"));
			paras.add(new TemplateParam("keyword2", orderTypeName, "#333"));
			paras.add(new TemplateParam("keyword3", sdf.format(orderInfoResult.getCreatedTime()), "#333"));// 下单时间
			paras.add(new TemplateParam("keyword4", sdf.format(new Date()), "#333"));// 取消时间
			template.setTemplateParamList(paras);
			new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info("==================== sendAutoCancelWaitOrderTemplate end==========================");

	}

	/**
	 * VIP到期提醒
	 */
	@Override
	public void sendVIPExpireTipsTemplate(UserInfo userInfo) {
		logger.info("=================>> sendVIPExpireTipsTemplate is run ");
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (userInfo != null) {
				Template template = new Template();
				template.setToUser(userInfo.getOpenid());
				template.setTemplateId(TemplateConstants.VIP_EXPIRE_TIPS);
				template.setTopColor("#333");
				//设置开通会员链接地址
				template.setUrl(TemplateConstants.TEMPLATE_TO_FOPEN_VIP_URL);
				List<TemplateParam> paras = new ArrayList<TemplateParam>();
				paras.add(new TemplateParam("first", "您好，您围裙健身年卡即将到期，为不影响自助健身的预约，请及时购买", "#333"));
				paras.add(new TemplateParam("name", "围裙健身年卡", "#333"));
				paras.add(new TemplateParam("expDate", sdf.format(DateUtils.AddDays(userInfo.getVipEndTime(),-1)), "#333"));
				paras.add(new TemplateParam("remark", "如有任何疑问，可联系客服：0755-86629280。", "#333"));
				template.setTemplateParamList(paras);
				new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
			}
		} catch (Exception e) {
			logger.error("================>> sendVIPExpireTipsTemplate has Error ! "+e.getMessage());
		}
	}

	@Override
	public void sendUpdateCourseNotice(String title, String courseAddr, Date updateDate, UserInfo userInfo) {
		logger.info("=================>> sendVIPExpireTipsTemplate is run ");
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (userInfo != null) {
				Template template = new Template();
				template.setToUser(userInfo.getOpenid());
				template.setTemplateId(TemplateConstants.CURRICULUM_SCHEDULE_UPDATE_TIPS);
				template.setTopColor("#333");
				//设置团课列表链接
				template.setUrl(TemplateConstants.TEMPLATE_TO_TEAM_COURSE_URL);
				List<TemplateParam> paras = new ArrayList<TemplateParam>();
				paras.add(new TemplateParam("first", title, "#333"));
				paras.add(new TemplateParam("keyword1",  sdf.format(updateDate), "#333"));
				paras.add(new TemplateParam("keyword2",courseAddr, "#333"));
				paras.add(new TemplateParam("remark", "点击查看课表详情", "#333"));
				template.setTemplateParamList(paras);
				new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
			}
		} catch (Exception e) {
			logger.error("================>> sendVIPExpireTipsTemplate has Error ! "+e.getMessage());
		}
	}
	
	/**
	 * VIP到期提醒
	 */
	@Override
	public void sendSelfExpireTipsTemplate(UserProductService userProductService) {
		logger.info("=================>> sendVIPExpireTipsTemplate is run ");
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (userProductService != null) {
				Template template = new Template();
				template.setToUser(userProductService.getOpenId());
				template.setTemplateId(TemplateConstants.VIP_EXPIRE_TIPS);
				template.setTopColor("#333");
				//设置开通会员链接地址
				template.setUrl(TemplateConstants.TEMPLATE_TO_FOPEN_VIP_URL);
				List<TemplateParam> paras = new ArrayList<TemplateParam>();
				paras.add(new TemplateParam("first", "您好，您围裙健身年卡即将到期，为不影响自助健身的预约，请及时购买", "#333"));
				paras.add(new TemplateParam("name", "围裙健身年卡", "#333"));
				paras.add(new TemplateParam("expDate", sdf.format(DateUtils.AddDays(userProductService.getEndDate(),-1)), "#333"));
				paras.add(new TemplateParam("remark", "如有任何疑问，可联系客服：0755-86629280。", "#333"));
				template.setTemplateParamList(paras);
				new Thread(new SendTemplateThread(template, tokenMapper,tokenRedisDao)).start();
			}
		} catch (Exception e) {
			logger.error("================>> sendVIPExpireTipsTemplate has Error ! "+e.getMessage());
		}
	}

}
